//
//  ZCEssenceCell.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/28.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCEssenceCell.h"
#import "ZCEssenceModel.h"
#import "ZCUserView.h"
#import "ZCMediaView.h"
#import "ZCTopCommentView.h"
#import "ZCStatusToolsView.h"

#define defaultMargin 10
@interface ZCEssenceCell ()


@property (weak, nonatomic) ZCUserView *userView;
@property (weak, nonatomic) UILabel *textLab;
@property (weak, nonatomic) ZCMediaView *mediaView;
@property (weak, nonatomic) ZCTopCommentView *topCommentView;
@property (weak, nonatomic) ZCStatusToolsView *statusToolView;

@property (weak, nonatomic) MASConstraint *topCommentViewConstraintHeight;
@property (weak, nonatomic) MASConstraint *topCommentViewConstraintTop;

@property (weak, nonatomic) MASConstraint *statusToolConstraint;

@end

@implementation ZCEssenceCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
        UIImageView *backgroud = [[UIImageView alloc]init];
        backgroud.image = [UIImage imageNamed:@"mainCellBackground"];
        self.backgroundView = backgroud;
        
    }
    return self;
}
-(void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ZCUserView *userView = [[ZCUserView alloc]init];
    self.userView =userView;
    userView.backgroundColor = [UIColor whiteColor];
    
    UILabel *textLab = [UILabel labelWithText:nil textColor:[UIColor blackColor] fontSize:15 textAlignment:NSTextAlignmentLeft];
    self.textLab = textLab;
    textLab.backgroundColor = [UIColor whiteColor];
    
    ZCMediaView *mediaView = [[ZCMediaView alloc]init];
    self.mediaView = mediaView;
    
    ZCTopCommentView *topCommentView = [[ZCTopCommentView alloc]init];
    self.topCommentView = topCommentView;
    topCommentView.backgroundColor = ZCRGBColor(240, 240, 240);
    
    ZCStatusToolsView *statusToolView = [[ZCStatusToolsView alloc]init];
    self.statusToolView = statusToolView;
    statusToolView.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:userView];
    [self.contentView addSubview:textLab];
    [self.contentView addSubview:mediaView];
    [self.contentView addSubview:topCommentView];
    [self.contentView addSubview:statusToolView];
    
    [userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.contentView);
    }];
        
    [textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(defaultMargin);
        make.trailing.equalTo(self.contentView).offset(-defaultMargin);
        make.top.equalTo(userView.mas_bottom);
        
    }];
    
    [mediaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLab.mas_bottom).offset(defaultMargin);
        make.leading.equalTo(self.contentView).offset(defaultMargin);
        make.trailing.equalTo(self.contentView).offset(-defaultMargin);

    }];
    
    [topCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.topCommentViewConstraintTop = make.top.equalTo(mediaView.mas_bottom);
        make.leading.equalTo(self.contentView).offset(defaultMargin);
        make.trailing.equalTo(self.contentView).offset(-defaultMargin);
//        self.topCommentViewConstraintHeight = make.height.mas_equalTo(30);//调试用
    }];
    
    [statusToolView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.statusToolConstraint = make.top.equalTo(topCommentView.mas_bottom);
        make.trailing.leading.equalTo(self.contentView);
        make.height.mas_equalTo(35);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self);
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.bottom.equalTo(statusToolView.mas_bottom);
    }];
    
    
}


-(void)setModel:(ZCEssenceModel *)model{
    _model = model;
    
    self.userView.model = model;
    self.textLab.text = model.text;
    self.mediaView.model = model;
    self.topCommentView.model = model;
    self.statusToolView.model = model;
    
    if (model.type.intValue == Text) {
        
        self.mediaView.hidden = YES;
        if (model.top_cmt.count >0) {
            
            self.topCommentView.hidden = NO;
            
//            !self.topCommentViewConstraintHeight? : [self.topCommentViewConstraintHeight uninstall];
            !self.topCommentViewConstraintTop?:[self.topCommentViewConstraintTop uninstall];
            [self.topCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.topCommentViewConstraintTop = make.top.equalTo(self.textLab.mas_bottom);
//                self.topCommentViewConstraintHeight = make.height.mas_equalTo(30);//调试用
            }];
            
            !self.statusToolConstraint? : [self.statusToolConstraint uninstall];
            [self.statusToolView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.statusToolConstraint = make.top.equalTo(self.topCommentView.mas_bottom);
            }];
        }else{
            self.topCommentView.hidden = YES;
            
            !self.statusToolConstraint? : [self.statusToolConstraint uninstall];
            [self.statusToolView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.statusToolConstraint =  make.top.equalTo(self.textLab.mas_bottom);
            }];
        }
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
    }else{
        
        self.mediaView.hidden = NO;
        if (model.top_cmt.count == 0) {
            self.topCommentView.hidden = YES;
            !self.statusToolConstraint? : [self.statusToolConstraint uninstall];
            [self.statusToolView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.statusToolConstraint = make.top.equalTo(self.mediaView.mas_bottom);
            }];
        }else{
            self.topCommentView.hidden = NO;

//            !self.topCommentViewConstraintHeight? : [self.topCommentViewConstraintHeight uninstall];
            !self.topCommentViewConstraintTop? :[self.topCommentViewConstraintTop uninstall];
            [self.topCommentView mas_makeConstraints:^(MASConstraintMaker *make) {
//                self.topCommentViewConstraintHeight = make.height.mas_equalTo(30);
                self.topCommentViewConstraintTop = make.top.equalTo(self.mediaView.mas_bottom);
            }];
            !self.statusToolConstraint? : [self.statusToolConstraint uninstall];
            [self.statusToolView mas_makeConstraints:^(MASConstraintMaker *make) {
                self.statusToolConstraint = make.top.equalTo(self.topCommentView.mas_bottom);
            }];
        }
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
    }
}

-(void)setFrame:(CGRect)frame{
    
//    frame.origin.y += 10;
//    frame.size.height += 10;
    
    [super setFrame:frame];
}


@end
